<?php
function adminLog($log_info,$log_type=0){
    $add['log_time'] = time();
    $add['admin_id'] = session('admin_id');
    $add['log_info'] = $log_info;
    $add['log_ip'] = getIP();
    $add['log_url'] = __ACTION__;
    $add['log_type'] = $log_type;
    M('admin_log')->add($add); 
}
 

function getAdminInfo($admin_id){
	return D('admin')->where("admin_id=$admin_id")->find();
}

function get_order_sn()
{
    // 保证不会有重复订单号存在
    while(true){
        $order_sn = date('YmdHis').rand(1000,9999); // 订单编号
        $order_sn_count = M('order')->where("order_sn = '$order_sn' or master_order_sn = '$order_sn'")->count();
        if($order_sn_count == 0)
            break;
    }
    return $order_sn;
}
function pic64($imgs){
$img = file_get_contents($imgs);
$data = base64_encode($img);
return 'data:image/png;base64,'.$data;
}
function tpversion()
{     
    if(!empty($_SESSION['isset_push']))
        return false;
    $_SESSION['isset_push'] = 1;
    error_reporting(0);//关闭所有错误报告
    $app_path = dirname($_SERVER['SCRIPT_FILENAME']).'/';
    $version_txt_path = $app_path.'/Application/Admin/Conf/version.txt';
    $curent_version = file_get_contents($version_txt_path);
    
    $vaules = array(
            'domain'=>$_SERVER['HTTP_HOST'], 
            'last_domain'=>$_SERVER['HTTP_HOST'], 
            'key_num'=>$curent_version, 
            'install_time'=>INSTALL_DATE, 
            'cpu'=>'0001',
            'mac'=>'0002',
            'serial_number'=>SERIALNUMBER,
            );
     $url = "http://v1.weixinfenxiaoshop.com/index.php?m=Home&c=Index&a=user_push&".http_build_query($vaules);
     stream_context_set_default(array('http' => array('timeout' => 3)));
     file_get_contents($url);
}
 
/**
 * 面包屑导航  用于后台管理
 * 根据当前的控制器名称 和 action 方法
 */
function navigate_admin()
{
    $navigate = include APP_PATH.'Common/Conf/navigate.php';   
    $location = strtolower('Admin/'.CONTROLLER_NAME);
    $arr = array(
        '后台首页'=>'javascript:void();',
        $navigate[$location]['name']=>'javascript:void();',
        $navigate[$location]['action'][ACTION_NAME]=>'javascript:void();',
    );
    return $arr;
}

/**
 * 导出excel
 * @param $strTable	表格内容
 * @param $filename 文件名
 */
function downloadExcel($strTable,$filename)
{
	header("Content-type: application/vnd.ms-excel");
	header("Content-Type: application/force-download");
	header("Content-Disposition:attachment; filename=".$filename."_".date('Y-m-d').".xls");
	header('Expires:0');
	header('Pragma:public');
	echo '<html><meta http-equiv="Content-Type" content="text/html;charset=utf-8" />'.$strTable.'</html>';
}

/**
 * 格式化字节大小
 * @param  number $size      字节数
 * @param  string $delimiter 数字和单位分隔符
 * @return string            格式化后的带单位的大小
 */
function format_bytes($size, $delimiter = '') {
	$units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
	for ($i = 0; $size >= 1024 && $i < 5; $i++) $size /= 1024;
	return round($size, 2) . $delimiter . $units[$i];
}

/**
 * 根据id获取地区名字
 * @param $regionId id
 */
function getRegionName($regionId){
    $data = M('region')->where(array('id'=>$regionId))->field('name')->find();
    return $data['name'];
}

function getMenuList($act_list){
	//根据角色权限过滤菜单
	$menu_list = getAllMenu();
	if($act_list != 'all'){
		$right = M('system_menu')->where("id in ($act_list)")->cache(true,7200)->getField('right',true);
		foreach ($right as $val){
			$role_right .= $val.',';
		}
		$role_right = explode(',', $role_right);
		foreach($menu_list as $k=>$mrr){
			foreach ($mrr['sub_menu'] as $j=>$v){
				if(!in_array($v['control'].'Controller@'.$v['act'], $role_right)){
					unset($menu_list[$k]['sub_menu'][$j]);//过滤菜单
				}
			}
		}
	}
	return $menu_list;
}


function groupMenu(){
	return array('system'=>'系统设置','admin'=>'权限管理','member'=>'会员管理','goods'=>'商品管理',
    			'order'=>'订单管理','store'=>'店铺管理','ad'=>'广告管理','marketing'=>'活动管理',
    			'distribut'=>'分销管理','tools'=>'插件管理','finance'=>'财务管理','count'=>'统计报表','content'=>'文章管理','pintuan'=>'拼团管理','jifen'=>'积分商城'
    	);
}

function getAllMenu(){
	$menuarr=array(
			'system' => array('name'=>'系统设置','icon'=>'fa-cog','sub_menu'=>array(
					array('name'=>'网站设置','act'=>'index','control'=>'System'),
					array('name'=>'首页图标导航','act'=>'navigationList','control'=>'System'),
					array('name'=>'会员中心图标','act'=>'user_nav','control'=>'System'),
					array('name'=>'底部菜单','act'=>'tab','control'=>'System'),
					array('name'=>'区域管理','act'=>'region','control'=>'Tools'),
					array('name'=>'权限资源列表','act'=>'right_list','control'=>'System'), 
					array('name'=>'支付插件','act'=>'index','control'=>'Plugin'),
                    // array('name'=>'线路管理','act'=>'xian_shop','control'=>'System'),
                    array('name'=>'活动模板列表','act'=>'template','control'=>'System'),
                    array('name'=>'线路管理','act'=>'line','control'=>'System'),

            )),
			'access' => array('name' => '权限管理', 'icon'=>'fa-gears', 'sub_menu' => array(
					array('name' => '<span style="color:#ff9800">分部管理</span>', 'act'=>'fenbu', 'control'=>'Admin'),
					array('name' => '管理员列表', 'act'=>'index', 'control'=>'Admin'),
					array('name' => '角色管理', 'act'=>'role', 'control'=>'Admin'),
					array('name' => '管理员日志', 'act'=>'log', 'control'=>'Admin'),
					array('name' => '商家日志', 'act'=>'sellerlog', 'control'=>'Admin'),
					//array('name' => '供应商列表', 'act'=>'supplier', 'control'=>'Admin'),
			)),
			'member' => array('name'=>'会员管理','icon'=>'fa-user','sub_menu'=>array(
					array('name'=>'会员列表','act'=>'index','control'=>'User'),
                    array('name' => '配送员列表', 'act' => 'driver', 'control' => 'User'),
                    array('name' => '业务员列表', 'act' => 'salesman', 'control' => 'User'),
					array('name'=>'会员等级','act'=>'levelList','control'=>'User'),
					array('name'=>'会员充值','act'=>'recharge','control'=>'User'),
					array('name'=>'投诉建议','act'=>'tousu','control'=>'User'),
					//array('name'=>'会员整合','act'=>'integrate','control'=>'User'),
			)),
			'goods' => array('name' => '商品管理', 'icon'=>'fa-tasks', 'sub_menu' => array(
					array('name' => '商品分类', 'act'=>'categoryList', 'control'=>'Goods'),
					array('name' => '商品列表', 'act'=>'goodsList', 'control'=>'Goods'),
					array('name' => '待审核商品列表', 'act'=>'goodsHoldList', 'control'=>'Goods'),
					array('name' => '<span style="color:#fb6e52">区域产品限制</span>', 'act'=>'goods_list_quyu', 'control'=>'Goods'),
					array('name' => '区域产品溢价', 'act'=>'goods_list_quyu_price', 'control'=>'Goods'),
//					array('name' => '商品类型', 'act'=>'goodsTypeList', 'control'=>'Goods'),
//					array('name' => '商品规格', 'act' =>'specList', 'control' => 'Goods'),
					array('name' => '品牌列表', 'act'=>'brandList', 'control'=>'Goods'),
			)),
			'order' => array('name' => '订单管理', 'icon'=>'fa-money', 'sub_menu' => array(
					array('name' => '订单列表', 'act'=>'index', 'control'=>'Order'),
					array('name' => '商家配货订单', 'act'=>'distribution', 'control'=>'Order'),
					array('name' => '<span style="color:#fb6e52">司机待配送单</span>', 'act'=>'index_sj', 'control'=>'Order'),
					array('name' => '司机已配送单', 'act'=>'index_sj_log', 'control'=>'Order'),
					// array('name' => '待发货单', 'act'=>'delivery_list', 'control'=>'Order'),
					//array('name' => '快递单', 'act'=>'express_list', 'control'=>'Order'),
					array('name' => '改单列表', 'act'=>'return_list', 'control'=>'Order'),
					array('name' => '入库没入库完', 'act'=>'ruku_count', 'control'=>'Order'),
//					array('name' => '退货单', 'act'=>'tuihuo_list', 'control'=>'Order'),
					array('name' => '<span style="color:#f88e2d">退货单(new)</span>', 'act'=>'tuihuo_list2', 'control'=>'Order'),
					array('name' => '维修/换货商品单', 'act'=>'fix_list', 'control'=>'Order'),
					// array('name' => '结算中心', 'act'=>'caiwu', 'control'=>'Caiwu'),
					//array('name' => '订单日志', 'act'=>'order_log', 'control'=>'Order'),
					// array('name' => '商品评论','act'=>'index','control'=>'Comment'),
//					array('name' => '商品咨询','act'=>'ask_list','control'=>'Comment'),
				
			)), 
			'Store' => array('name' => '店铺管理', 'icon'=>'fa-home', 'sub_menu' => array(
					array('name' => '店铺等级', 'act'=>'store_grade', 'control'=>'Store'),
					array('name' => '店铺分类', 'act'=>'store_class', 'control'=>'Store'),
					array('name' => '店铺列表', 'act'=>'store_list', 'control'=>'Store'),
					array('name' => '<span style="color:#fb6e52">区域限制店铺</span>', 'act'=>'store_list_quyu', 'control'=>'Store'),
//					array('name' => '区域价格店铺', 'act'=>'store_list_quyu_price', 'control'=>'Store'),
					array('name' => '区域溢价店铺', 'act'=>'store_list_yijia_quyu', 'control'=>'Store'),
					//array('name' => '自营店铺', 'act'=>'store_own_list', 'control'=>'Store'),
					//array('name' => '经营类目审核', 'act'=>'apply_class_list', 'control'=>'Store'),
			)),
			'Ad' => array('name' => '广告管理', 'icon'=>'fa-flag', 'sub_menu' => array(
					array('name' => '广告列表', 'act'=>'adList', 'control'=>'Ad'),
					array('name' => '广告位置', 'act'=>'positionList', 'control'=>'Ad'),
			)),	
			 'jifen' => array('name' => '积分商城', 'icon' => 'fa-paste', 'sub_menu' => array(
			 		array('name' => '类别管理', 'act' => 'typeList', 'control' => 'Jifen'),
			 		array('name' => '商品管理', 'act' => 'goodsList', 'control' => 'Jifen'),
			 		array('name' => '订单管理', 'act' => 'orderList', 'control' => 'Jifen'),
			 		array('name' => '发货设置', 'act' => 'shipping_list?type=shipping&code=yuantong', 'control' => 'Jifen'),
			 		array('name' => '发货', 'act' => 'delivery_list', 'control' => 'Jifen'),
//					 array('name' => '商品评论', 'act' => 'index?ty=1', 'control' => 'comment'),
//					 array('name' => '退货单', 'act' => 'return_list?ty=1', 'control' => 'order'),
			)),
			'promotion' => array('name' => '活动管理', 'icon'=>'fa-bell', 'sub_menu' => array(
					// array('name' => '秒杀管理', 'act'=>'index', 'control'=>'miaosha'),
					// array('name' => '抢购管理', 'act'=>'flash_sale', 'control'=>'Promotion'),
					// array('name' => '团购管理', 'act'=>'group_buy_list', 'control'=>'Promotion'),
					array('name' => '转盘中奖', 'act'=>'zp_list', 'control'=>'Promotion'),
				// 	array('name' => '<span style="color:red">未审活动报名</span>', 'act'=>'join_goods_list?status=0', 'control'=>'Promotion'),
				// 	array('name' => '<span style="color:green">已审活动报名</span>', 'act'=>'join_goods_list?status=1', 'control'=>'Promotion'),
					array('name' => '全店促销', 'act'=>'prom_store_list', 'control'=>'Promotion'),
					array('name' => '商家活动', 'act'=>'prom_goods_list', 'control'=>'Promotion'),
					array('name' => '产品活动', 'act'=>'prom_goods', 'control'=>'Promotion'),
					array('name' => '平台满赠', 'act'=>'prom_full_list', 'control'=>'Promotion'),
					// array('name' => '订单促销', 'act'=>'prom_order_list', 'control'=>'Promotion'),
					array('name' => '平台优惠券','act'=>'index', 'control'=>'Coupon'),
					array('name' => '每日签到','act'=>'sign_in', 'control'=>'Promotion'),
					array('name' => '活动统计','act'=>'huodongguolv', 'control'=>'Promotion'),
					array('name' => '每日秒杀','act'=>'daily_goods_list', 'control'=>'Promotion'),
                    array('name' => '直播','act'=>'live_list', 'control'=>'Live'),
			)),
			
			
			'distribut' => array('name' => '分销管理', 'icon'=>'fa-cubes', 'sub_menu' => array(
					array('name' => '分销设置', 'act'=>'set', 'control'=>'Distribut'),
					array('name' => '分销商列表', 'act'=>'distributor_list', 'control'=>'Distribut'),
					array('name' => '分销关系', 'act'=>'tree', 'control'=>'Distribut'),
				/* 	array('name' => '提现申请', 'act'=>'withdrawals', 'control'=>'Distribut'), */
					array('name' => '分成日志', 'act'=>'rebate_log', 'control'=>'Distribut'),
			)),			
            
			'tools' => array('name' => '插件工具', 'icon'=>'fa-plug', 'sub_menu' => array(
					//array('name' => '清除演示数据', 'act'=>'cleandemo', 'control'=>'System'),
					array('name' => '数据备份', 'act'=>'index', 'control'=>'Tools'),
					array('name' => '数据还原', 'act'=>'restore', 'control'=>'Tools'),
			)),
			'finance' => array('name' => 'PDA管理', 'icon'=>'fa-book', 'sub_menu' => array(
					// array('name' => '商家对账/结算', 'act'=>'store_duizhang', 'control'=>'Finance'),
					// array('name' => '商家汇款记录', 'act'=>'store_remittance', 'control'=>'Finance'),
					// array('name' => '会员提现申请', 'act'=>'withdrawals', 'control'=>'Finance'),
					// array('name' => '会员汇款记录', 'act'=>'remittance', 'control'=>'Finance'),
					// array('name' => 'PDA跟踪收款', 'act'=>'pda', 'control'=>'Finance'),
					array('name' => 'PDA入库但未出库', 'act'=>'noruku_pda', 'control'=>'Finance'),
					//array('name' => 'PDA出库但未入库', 'act'=>'nochuku_pda', 'control'=>'Finance'),
					array('name' => '<span style="color:#f88e2d">PDA入库明细</span>', 'act'=>'ruku_pda', 'control'=>'Finance'),
					array('name' => 'PDA出库明细', 'act'=>'chuku_pda', 'control'=>'Finance'),
//					array('name' => 'PDA收款明细', 'act'=>'pdas', 'control'=>'Finance'),
					array('name' => '入库二维码', 'act'=>'pda_qr_show', 'control'=>'Finance'),
					// array('name' => '商家未结算单', 'act'=>'order_no_statis', 'control'=>'Finance'),
					// array('name' => '商家结算记录', 'act'=>'order_statis', 'control'=>'Finance'),
			)),'finances' => array('name' => '结算/收款', 'icon'=>'fa-book', 'sub_menu' => array(
					// array('name' => 'PDA跟踪收款', 'act'=>'pda', 'control'=>'Finance'),
					//array('name' => '<span style="color:#f88e2d">总部/分部对账</span>', 'act'=>'pda_duizhang', 'control'=>'Finance'),
					array('name' => '商家对账/结算', 'act'=>'store_duizhang', 'control'=>'Finance'),
					array('name' => '商家结算记录', 'act'=>'store_withdrawals', 'control'=>'Finance'),
				// 	array('name' => '商家自动收款', 'act'=>'ordersettlement', 'control'=>'Finance'),
			)),'financese' => array('name' => 'PDA收款', 'icon'=>'fa-book', 'sub_menu' => array(
					array('name' => 'PDA跟踪收款', 'act'=>'pda_new', 'control'=>'Finance'),
					array('name' => '<span style="color:#f88e2d">PDA跟踪收款(new)</span>', 'act'=>'pda_new2', 'control'=>'Finance'),
					array('name' => '<span style="color:#f88e2d">溢价收益</span>', 'act'=>'yijia', 'control'=>'Finance'),
					array('name' => '<span style="color:#ef1313">异常订单</span>', 'act'=>'unusual', 'control'=>'Finance'),
                    array('name' => '<span style="color:#f88e2d">线上收款对数</span>', 'act'=>'payOnlineCount', 'control'=>'Finance'),
                    array('name' => '<span style="color:#f88e2d">线上支付余额记录</span>', 'act'=>'deduct', 'control'=>'Finance'),
				// 	array('name' => '<span style="color:#f88e2d">财务对数</span>', 'act'=>'storeCount', 'control'=>'Finance'),
					// array('name' => 'PDA收款记录', 'act'=>'order_statis', 'control'=>'Finance'),
					// array('name' => '商家对账/结算', 'act'=>'store_duizhang', 'control'=>'Finance'),
					// array('name' => '商家结算记录', 'act'=>'store_withdrawals', 'control'=>'Finance'),
			)),
			'count' => array('name' => '统计报表', 'icon'=>'fa-signal', 'sub_menu' => array(
					array('name' => '销售概况', 'act'=>'index', 'control'=>'Report'),
					array('name' => '浏览记录', 'act'=>'liulan', 'control'=>'Report'),
					array('name' => '单品销售排行', 'act'=>'saleTop', 'control'=>'Report'),
					array('name' => '线路会员消费排行', 'act'=>'userTop_xian', 'control'=>'Report'),
					array('name' => '会员消费排行', 'act'=>'userTop', 'control'=>'Report'),
					array('name' => '商家销售排行', 'act'=>'saleList', 'control'=>'Report'),
					array('name' => '会员统计', 'act'=>'user', 'control'=>'Report'),
					array('name' => '<span style="color:green">没有消费会员</span>', 'act'=>'userTop_no', 'control'=>'Report'), 
					array('name'=>'产品访客','act'=>'youke','control'=>'Report'),
					array('name'=>'特推好货商品统计','act'=>'yanzhiorder','control'=>'Report'),
				// 	array('name'=>'满减商品统计','act'=>'fullReduction','control'=>'Report'),
					// array('name' => '运营概览', 'act'=>'finance', 'control'=>'Report'),
			)),
			'article' => array('name' => '文章管理', 'icon' => 'fa-paste', 'sub_menu' => array(
					array('name' => '文章列表', 'act' => 'articleList', 'control' => 'Article'),
					array('name' => '文章分类', 'act' => 'categoryList', 'control' => 'Article'),
			)),
			// 'wechat' => array('name' => '微信管理', 'icon' => 'fa-paste', 'sub_menu' => array(
			// 		array('name' => '微信菜单', 'act' => 'menu', 'control' => 'wechat'),
			// 		array('name' => '文章分类', 'act' => 'categoryList', 'control' => 'Article'),
			// )),
	); 
	if(session('fenbu_id')!=0){
		if($menuarr['access']['sub_menu'][0]['act']=='fenbu'){
			unset($menuarr['access']['sub_menu'][0]);unset($menuarr['goods']['sub_menu'][0]);unset($menuarr['goods']['sub_menu'][4]);unset($menuarr['goods']['sub_menu'][4]);unset($menuarr['Store']['sub_menu'][4]);unset($menuarr['Store']['sub_menu'][3]); //unset($menuarr['goods']['sub_menu'][3]);
			unset($menuarr['system']);unset($menuarr['Ad']);unset($menuarr['tools']);unset($menuarr['promotion']);unset($menuarr['article']);unset($menuarr['distribut']);unset($menuarr['financese']['sub_menu'][1]);
		}
			
	}
		
	
	require './Application/Admin/Common/function_extra.php';
	$rarr=getAllMenu_extra($menuarr);
	return $rarr;
}

function getAdlink(){
    $menuarr=	array(
        "/pages/index/index"=>"商城首页",
        "/pages/goods_details/goods_details"=>"商品详情",
        "/pages/store-cate/store-cate"=>"店铺街",
        "/pages/goods/list/list?keywords=is_new"=>"天天特价",
        "/pages/goods/list/list?keywords=is_recommend"=>"好货推荐",
        "/pages/user_coupon/user_coupon"=>"我的优惠券",
        "/pages/user_order/user_order"=>"订单列表",
        "/pages/bundle/user_collection/user_collection"=>"我的收藏",
        "/pages/integral_mall/index/index"=>"积分商城",
        "/pages/topic-list/topic-list"=>"文章列表",
        "/pages/topic/topic"=>"文章详情",
        "/pages/store/index"=>"店铺详情",
        "/pages/goods_search/goods_search"=>"品牌列表",
        "/pages/bundle/activity_list/activity_list"=>"活动专区",
        
    );
    require './Application/Admin/Common/function_extra.php';
    $rarr=getAdlink_extra($menuarr);
    return $rarr;
}
function cart_price($goods_id=0){ //更新购物车
			$goods=M('cart')->where('goods_id='.$goods_id)->select();//更新购物车
			
			foreach($goods as $v){
			$new_price=get_goods_huodong($v['goods_id'],$v['spec_key']);
				if($new_price){
					M('cart')->where('goods_id='.$v['goods_id'].' and `spec_key` = "'.$v['spec_key'].'"')->setField([
					'spec_key_name'=>$new_price["key_name"],
					'goods_name'=>$new_price["goods_name"],
					'goods_price'=>sanwei($new_price['price']),
					'member_goods_price'=>sanwei($new_price['price'])
					]);
				}else{
					M('cart')->where('goods_id='.$v['goods_id'].' and `spec_key` = "'.$v['spec_key'].'"')->delete();
				}
			}
}

function getsystem_nav(){
	
	$menuarr=	array(
               '/pages/category/category' => '商品分类',
               '/pages/cart/cart' => '购物车',
               '/pages/member/coupon/index' => '优惠劵',
			   '/pages/member/collect/collect' => '我的收藏',
			   '/pages/apply/apply' => '申请入驻',
			   '/pages/goods/grouplist/list' => '团购',
			   '/pages/order/list/list' => '我的订单',
               '/pages/member/coupon-list/coupon-list' => '领取优惠券',
			   '/pages/store-cate/store-cate' => '店铺街',
               '/pages/topic-list/topic-list' => '文章列表',
	           '/pages/credit/credit' => '积分商城',
			   '/pages/signin/signin' => '签到',
				"/pages/member/index/index"=>"会员中心",
				"/pages/index/index"=>"首页",
           ); 
	require './Application/Admin/Common/function_extra.php';
	$rarr=getsystem_nav_extra($menuarr);
	return $rarr;
}

function get_usercenter_nav(){

  $menuarr=	 array(
        '/pages/user_coupon/user_coupon' => '我的优惠券',
        '/pages/member/coupon-list/coupon-list' => '领券中心',
        '/pages/bundle/user_collection/user_collection' => '我的收藏',
        '/pages/address/list/list' => '地址管理',
        '/pages/apply/apply' => '申请入驻',
        '/pages/recharge/recharge' => '充值中心',
        '/pages/distribute/index' => '分销中心',
        '/pages/member/point/point' => '积分明细',
        '/pages/credit/credit' => '积分商城',
        '/pages/signin/signin' => '签到',
        '/pages/topic-list/topic-list' => '文章列表',
        '/pages/bundle/server_explan/server_explan?name=关于我们&amp;type=4' => '关于我们',
        '/pages/bundle/server_explan/server_explan?name=用户协议&type=4' => '用户协议',
        '/pages/bundle/server_explan/server_explan?name=隐私政策&type=4' => '隐私政策',
        '/pages/bundle/server_explan/server_explan?name=积分说明&type=4' => '积分说明',
        '/pages/bundle/server_explan/server_explan?name=商家规则&type=4' => '商家规则',
        '/pages/bundle/server_explan/server_explan?name=退换货原则&type=4' => '退换货原则',
        '/pages/bundle/server_explan/server_explan?name=平台资质&type=4' => '平台资质',
        '/pages/bundle/user_records/user_records' => '浏览记录',
        '/pages/bundle/complaint/complaint' => '意见反馈',
        'contact' => '客服'	,
    );
    require './Application/Admin/Common/function_extra.php';
    $rarr=get_usercenter_nav_extra($menuarr);
    return $rarr;

}


function respose($res){
	exit(json_encode($res));
}

function arraySort($array,$keys,$sort='asc') {
    $newArr = $valArr = array();
    foreach ($array as $key=>$value) {
        $valArr[$key] = $value[$keys]; 
    }
    ($sort == 'asc') ?  asort($valArr) : arsort($valArr);
    reset($valArr);
    foreach($valArr as $key=>$value) {
        $newArr[$key] = $array[$key];
    }
    return $newArr;
}



/**
 * 生成推流地址
 * @param $push_domain 推流域名
 * @param $push_key 推流域名配置的鉴权Key
 * @param $expireTime 过期时间（单位是秒）
 * @param $appName 推流AppName
 * @param $streamName 推流StreamName
 */
function push_url($push_domain,$push_key,$expireTime=3600,$appName,$streamName){

    $push_url = '';
    //未开启鉴权Key的情况下
    if($push_key==''){
        $push_url = 'rtmp://'.$push_domain.'/'.$appName.'/'.$streamName;
        return $push_url;
    }
    $timeStamp = time() + $expireTime;
    $sstring = '/'.$appName.'/'.$streamName.'-'.$timeStamp.'-0-0-'.$push_key;
    $md5hash = md5($sstring);
    $push_url = 'rtmp://'.$push_domain.'/'.$appName.'/'.$streamName.'?auth_key='.$timeStamp.'-0-0-'.$md5hash;
    return $push_url;
}


/**
 * 生成播放地址
 * @param $play_domain 播放域名
 * @param $play_key 播放鉴权Key
 * @param $expireTime 过期时间（单位是秒）
 * @param $appName 播放appName（同推流appName)
 * @param $streamName 播放streamName，播放源流时，streamName 同推流streamName；播放转码流时，streamName 为推流streamName_{转码模板ID}
 */
function play_url($play_domain,$play_key,$expireTime=3600,$appName,$streamName){
    if($play_key==''){
        $rtmp_play_url = 'rtmp://'.$play_domain.'/'.$appName.'/'.$streamName;
        $flv_play_url = 'http://'.$play_domain.'/'.$appName.'/'.$streamName.'.flv';
        $hls_play_url = 'http://'.$play_domain.'/'.$appName.'/'.$streamName.'.m3u8';
    }else{
        $timeStamp = time() + $expireTime;

        $rtmp_sstring = '/'.$appName.'/'.$streamName.'-'.$timeStamp.'-0-0-'.$play_key;
        $rtmp_md5hash = md5($rtmp_sstring);
        $rtmp_play_url = 'rtmp://'.$play_domain.'/'.$appName.'/'.$streamName.'?auth_key='.$timeStamp.'-0-0-'.$rtmp_md5hash;

        $flv_sstring = '/'.$appName.'/'.$streamName.'.flv-'.$timeStamp.'-0-0-'.$play_key;
        $flv_md5hash = md5($flv_sstring);
        $flv_play_url = 'http://'.$play_domain.'/'.$appName.'/'.$streamName.'.flv?auth_key='.$timeStamp.'-0-0-'.$flv_md5hash;

        $hls_sstring = '/'.$appName.'/'.$streamName.'.m3u8-'.$timeStamp.'-0-0-'.$play_key;
        $hls_md5hash = md5($hls_sstring);
        $hls_play_url = 'http://'.$play_domain.'/'.$appName.'/'.$streamName.'.m3u8?auth_key='.$timeStamp.'-0-0-'.$hls_md5hash;
    }


    return $rtmp_play_url;
}